import { createRouter, createWebHashHistory } from "vue-router";
// import HomeView from '../views/HomeView.vue'
// import store from '@/store/index.js'
// import {mianStore} from '@/store'
// import bus from '@/utils/mitt'
// import { mainStore } from "@/store/index.js";
const routes = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/login",
    name: "login",
    // component: HomeView
    component: () => import("../views/login/login.vue"),
  },
  {
    path: "/index",
    name: "index",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ "../views/home/home.vue"),
    children: [
      {
        path: "/index",
        name: "index",
        component: () => import("../views/home/content/index/index.vue"),
      },
      {
        path: "/product",
        name: "product",
        component: () => import("../views/home/content/product/index.vue"),
      },
      {
        path: "/census",
        name: "census",
        component: () => import("../views/home/content/census/index.vue"),
      },
      {
        path: "/permission",
        name: "permission",
        component: () => import("../views/home/content/permission/index.vue"),
      },
      {
        path: "/dayjs",
        name: "dayjs",
        component: () => import("../views/home/content/dayjs/index.vue"),
      },
      {
        path: "/lodash",
        name: "lodash",
        component: () => import("../views/home/content/lodash/index.vue"),
      }
    ],
  },
  {
    path: "/base",
    name: "base",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ "../views/home/home.vue"),
    children: [
      {
        path: "/button",
        name: "button",
        component: () => import("../views/home/base/button/index.vue"),
      },
      {
        path: "/layout",
        name: "layout",
        component: () => import("../views/home/base/layout/index.vue"),
      },
    ],
  },
  {
    path: "/echart",
    name: "echart",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ "../views/home/home.vue"),
    children: [
      {
        path: "/pie",
        name: "pie",
        component: () => import("../views/home/chart/pie/index.vue"),
      },
      {
        path: "/echarts",
        name: "echarts",
        component: () => import("../views/home/chart/index.vue"),
      }
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach(async (to, from, next) => {
  console.log('to, from', to, from)
  if (to.path == "/login") {
    next();
  } else {
    // bus.emit('routeChange')
    next();
  }
  // 要去的页面路由包含admin，我们就处理
  // 如果仓库中菜单的长度为0
  // if(to.path.includes("/admin")&&store.state.permission.menus.length===0){
  //   // 重新执行dispath
  //   // 执行getMenu这个action是需要时间的
  //   await store.dispatch("getMenu"); //等待 dispatch 执行完毕（reslove）再执行 next
  //   // 执行到下面这句的时候getMenu还没有执行完毕，就执行了next（导致递归重复执行）
  //   // 解决办法是：等待getMenu执行完毕后再执行next：2种方案1回调函数，2是promise
  //   next({path:to.fullPath})

  // }else{
  //   // 否则直接放行
  //   next();
  // }
});

export default router;
